package com.easylinkin.linkappapi.common.utils;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringHelper {
    public StringHelper() {
    }
    private static Pattern PATTERNCASE = Pattern.compile("([A-Za-z\\d]+)(_)?");
    private static Pattern PATTERN = Pattern.compile("[A-Z]([a-z\\d]+)?");

    public static String underline2Camel(String line, boolean smallCamel) {
        if (line != null && !"".equals(line)) {
            StringBuffer sb = new StringBuffer();
            Matcher matcher = PATTERNCASE.matcher(line);

            while(matcher.find()) {
                String word = matcher.group();
                sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0)) : Character.toUpperCase(word.charAt(0)));
                int index = word.lastIndexOf(95);
                if (index > 0) {
                    sb.append(word.substring(1, index).toLowerCase());
                } else {
                    sb.append(word.substring(1).toLowerCase());
                }
            }

            return sb.toString();
        } else {
            return "";
        }
    }

    public static String camel2Underline(String line) {
        if (line != null && !"".equals(line)) {
            line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
            StringBuffer sb = new StringBuffer();
            Matcher matcher = PATTERN.matcher(line);

            while(matcher.find()) {
                String word = matcher.group();
                sb.append(word.toUpperCase());
                sb.append(matcher.end() == line.length() ? "" : "_");
            }

            return sb.toString();
        } else {
            return "";
        }
    }

    public static String upperFirstChar(String str) {
        str.replace(str.substring(0, 1), str.substring(0, 1).toUpperCase());
        return str;
    }

    public static String lowerFirstChar(String str) {
        str = String.format("%s%s", str.substring(0, 1).toLowerCase(), str.substring(1));
        return str;
    }

    public static String newUniqueId() {
        long uniqueId = newLongUniqueId();
        return String.valueOf(uniqueId);
    }

    public static long newLongUniqueId() {
        long t1 = 2147483647L & System.currentTimeMillis();
        return t1 << 32 | (long)Math.abs((new Random()).nextInt());
    }

    public static boolean isNotEmpty(String s) {
        return !isEmpty(s);
    }

    public static boolean isEmpty(String s) {
        return s == null || s == "";
    }

    public static String genStringBySeparator(String str) {
        if (isEmpty(str)) {
            return str;
        } else {
            str = String.format("'%s'", str);
            str = str.replaceAll(",", "','");
            return str;
        }
    }

    public static String toStr(Object obj) {
        return toStr(obj, "");
    }

    public static String toStr(Object obj, String defaultValue) {
        if (obj == null) {
            return defaultValue;
        } else {
            try {
                String str = String.valueOf(obj);
                return str;
            } catch (RuntimeException var3) {
                return defaultValue;
            }
        }
    }

    public static int toInt(Object obj) {
        return toInt(obj, 0);
    }

    public static int toInt(Object obj, int defaultValue) {
        if (obj == null) {
            return defaultValue;
        } else {
            try {
                String str = String.valueOf(obj);
                return Integer.parseInt(str);
            } catch (RuntimeException var3) {
                return defaultValue;
            }
        }
    }
}
